//
//  NewsDetailViewController.m
//  UESTCPioneer
//
//  Created by sss on 14-9-26.
//  Copyright (c) 2014年 Sway. All rights reserved.
//

#import "NewsDetailViewController.h"

#define kBackColor [UIColor colorWithRed:235.0/255.0 green:235.0/255.0 blue:235.0/255.0 alpha:1]
#define KSPEColor [UIColor colorWithRed:204.0/255.0 green:204.0/255.0 blue:204.0/255.0 alpha:1]
@interface NewsDetailViewController ()<UIScrollViewDelegate>{
    NSArray *imgArray;
    NSString *title;
    NSString *content;
    
    UITextView *contentTextVeiw;
    
    CGFloat imgViewHeight;
}

@end

@implementation NewsDetailViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

-(id)initWithImgUrlArr:(NSArray *)imgUrlArr title:(NSString *)titleStr content:(NSString *)contentStr{
    self = [super init];
    if (self) {
        imgArray = [NSArray arrayWithArray:imgUrlArr];
        title = titleStr;
        content = contentStr;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithRed:235.0/255.0 green:235.0/255.0 blue:235.0/255.0 alpha:1];
    
    self.title = @"详情";
    
    [self setImgViewHeight];
    
    [self addTitleLabel];
    [self addTextView];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if (imgArray.count > 0) {
        [self addScrollView];
    }
    if (imgArray.count > 1) {
        [self addPageController];
    }
}

#pragma mark -
#pragma mark imgView

-(void)addScrollView{

    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, imgViewHeight + 20)];
    
    view.backgroundColor = kBackColor;
    
    
    UIView *bar1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 8)];
    bar1.backgroundColor = KSPEColor;
    [view addSubview:bar1];
    
    UIView *bar2 = [[UIView alloc] initWithFrame:CGRectMake(0, imgViewHeight + 8, 320, 12)];
    bar2.backgroundColor = KSPEColor;
    [view addSubview:bar2];
    
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    scrollView.frame = CGRectMake(0, 8, 320, imgViewHeight);
    scrollView.pagingEnabled = YES;
    scrollView.delegate = self;
    scrollView.contentSize = CGSizeMake(320 * imgArray.count, imgViewHeight);
    [scrollView setShowsHorizontalScrollIndicator:NO];
    for (int i = 0; i < imgArray.count; i ++) {
        NSString *picUrlStr = [baseUrl stringByAppendingString:imgArray[i]];
        UIImageView *imgView = [[UIImageView alloc] init];
        imgView.frame = CGRectMake(320 * i, 0, 320, imgViewHeight);
        imgView.contentMode = UIViewContentModeScaleAspectFit;
        [imgView setImageWithURL:[NSURL URLWithString:picUrlStr]];
        
        [scrollView addSubview:imgView];
    }
    [view addSubview:scrollView];
    
    [contentTextVeiw addSubview:view];
    
    
    
}

//根据图片的大小来设置imgView的高度
-(void)setImgViewHeight{
    if (imgArray.count <=0) {
        imgViewHeight = 0;
        return;
    }
    
    NSMutableArray *imgs = [[NSMutableArray alloc]init];
    for (int i = 0; i < imgArray.count; i++) {
        NSString *picUrlStr = [baseUrl stringByAppendingString:imgArray[i]];
        UIImageView *imgView = [[UIImageView alloc] init];
        imgView.contentMode = UIViewContentModeScaleAspectFit;
        [imgView setImageWithURL:[NSURL URLWithString:picUrlStr]];
        if (imgView.image == nil) {
            break;
        }
        [imgs addObject:imgView.image];
    }
    imgViewHeight = 100;
    CGFloat maxImgH = 0;
    for (int i = 0; i < imgs.count; i++) {
        UIImage *img = imgs[i];
        maxImgH = (maxImgH < img.size.height) ? img.size.height : maxImgH;
    }
    
    if (maxImgH/3 > 100 && maxImgH/3<=200) {
        imgViewHeight = maxImgH / 3;
        return;
    }
    if (maxImgH/3 > 200) {
        imgViewHeight = 200;
    }
}

-(void)addTitleLabel{
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.frame = CGRectMake(0, 0, 320, 61);
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.textColor = kNavigationBarColor;
    titleLabel.backgroundColor = kBackColor;
    titleLabel.font = [UIFont boldSystemFontOfSize:18.0f];
    titleLabel.text = title;
    titleLabel.numberOfLines = 0;
    
    UIView *bar = [[UIView alloc] initWithFrame:CGRectMake(0, 60, 320, 1)];
    bar.backgroundColor = KSPEColor;
    [titleLabel addSubview:bar];
    [self.view addSubview:titleLabel];
}

-(void)addTextView{
    contentTextVeiw = [[UITextView alloc]init];
    //------------ Sakura-2014-10-09 begin 修改-------------
    contentTextVeiw.frame = CGRectMake(0, 61, 320, self.view.frame.size.height - 125);
    //------------ Sakura end ---------------
    
    if (imgArray.count > 0) {
        contentTextVeiw.textContainerInset = UIEdgeInsetsMake(imgViewHeight + 20, 0, 0, 0);
    }

    contentTextVeiw.textColor = [UIColor darkGrayColor];
    contentTextVeiw.backgroundColor = kBackColor;
    contentTextVeiw.font = [UIFont systemFontOfSize:17];
    contentTextVeiw.textAlignment = NSTextAlignmentJustified;
    contentTextVeiw.text = content;
    contentTextVeiw.editable = NO;
    contentTextVeiw.scrollEnabled = YES;
    
    [self.view addSubview:contentTextVeiw];
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    int page = (int)(scrollView.contentOffset.x / 320);
    UIPageControl *pageC = (UIPageControl*)[contentTextVeiw viewWithTag:111];
    pageC.currentPage = page;
}

//add page controller
-(void)addPageController{
    UIPageControl *pageContr = [[UIPageControl alloc] initWithFrame:CGRectMake(0, imgViewHeight + 8, 320, 12)];
    pageContr.numberOfPages = imgArray.count;
    pageContr.tag = 111;
    [contentTextVeiw addSubview:pageContr];
}


@end
